/**
 * All Rights Reserved , Copyright (C) 2021 , 青岛鼎信通讯股份有限公司
 *
 *MpmProjectDemandController
 * 项目需求
 *
 * 修改纪录
 * 2021-08-04 版本：1.0 houyuhang 创建。
 * @version 版本：1.0
 * @author 作者：houyuhang
 * 创建日期：2021-08-04
 */

package com.topscomm.mpm.controller;

import java.util.*;

import com.topscomm.basic.util.Config;
import com.topscomm.cbo.pojo.CboApprovalNodeInsEntity;
import com.topscomm.cbo.service.impl.CboApprovalNodeInsService;
import com.topscomm.mpm.pojo.MpmProjectDemandEntity;
import com.topscomm.pub.util.*;
import com.topscomm.pub.vo.Page;
import com.topscomm.pub.vo.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.topscomm.basic.util.SQLAdapter;
import com.topscomm.mpm.controller.auto.MpmProjectDemandControllerAuto;
import com.topscomm.pub.vo.ResponseResult;
import com.topscomm.tap.common.TapSystemConst.TapOperationTypeEnum;

/**
 * @author: houyuhang
 * @date: 2021-08-04
 * @description:项目需求Controller
 * @modify:
 */
@RestController
@RequestMapping("mpm/projectDemand")
public class MpmProjectDemandController extends MpmProjectDemandControllerAuto implements IMpmProjectDemandController {
	@Autowired
	CboApprovalNodeInsService cboApprovalNodeInsService;
	/**
	 * @author: houyuhang
	 * @date: 2021-08-04
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	protected String spellListSql(Map<String, Object> paraMap) {
		StringBufferProxy sql = new StringBufferProxy();
		sql.appendSingle(" enabled=1 ");
		//单据的id
		if (paraMap.containsKey("qry_id")) {
			String value = paraMap.get("qry_id").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and id in ({0})", value);
		}
		//申请人姓名或工号
		if (paraMap.containsKey("qry_user")) {
			String value = paraMap.get("qry_user").toString();
			if (!StringUtil.isEmpty(value))
				if(value.matches("^[0-9]*$") || value.matches(".*\\d+.*")){
					sql.appendSingle(" and " + MpmProjectDemandEntity.FieldUsercode + " like '{0}%'", value);
				}else{
					sql.appendSingle(" and " + MpmProjectDemandEntity.FieldUsername + " like '%{0}%'", value);
				}
		}
		//项目名
		if (paraMap.containsKey("qry_projectcode")) {
			String value = paraMap.get("qry_projectcode").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and projectcode = '{0}'", value);
		}
		if (paraMap.containsKey("qry_state")) {
			String value = paraMap.get("qry_state").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and state={0}", value);
		}
		if (paraMap.containsKey("qry_docno")) {
			String value = paraMap.get("qry_docno").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and docno like '%{0}%'", value);
		}
		if (paraMap.containsKey("qry_begindocdate")) {
			String value = paraMap.get("qry_begindocdate").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and docdate >= {0} ", SQLAdapter.toDateLong(value + " 00:00:00"));
		}
		if (paraMap.containsKey("qry_enddocdate")) {
			String value = paraMap.get("qry_enddocdate").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and docdate <= {0}", SQLAdapter.toDateLong(value + " 23:59:59"));
		}
		if (paraMap.containsKey("qry_deptname")) {
			String value = paraMap.get("qry_deptname").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and (deptid in ( SELECT id from CboDept where fullname like '%{0}%'))", value);
		}
		if (paraMap.containsKey("qry_username")) {
			String value = paraMap.get("qry_username").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and (username like '%{0}%')", value);
		}
		if (paraMap.containsKey("qry_deptcode")) {
			String value = paraMap.get("qry_deptcode").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and deptcode like '{0}%'", value);
		}
		if (paraMap.containsKey("qry_state")) {
			String value = paraMap.get("qry_state").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and state={0}", value);
		}
		if (paraMap.containsKey("qry_where")) {
			String value = paraMap.get("qry_where").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and {0}", value);
		}
		if (this.getUserBean().getUser().getAdminlevel() < 1)// 管理员、超级管理员，不控制权限
		{
			String dataPession = service.getDataPermission(TapOperationTypeEnum.Select, "", this.getUserBean());
			if (StringUtil.isEmpty(dataPession))// 默认权限只能看本人，部门负责人为自己的部门和下级部门的数据
			{
				//state 2 0 为 退回和开立状态单据 只能本人查看
				sql.appendSingle(" and (CASE WHEN ((MpmProjectDemand.state = 0 OR MpmProjectDemand.state = 2 ) AND MpmProjectDemand.USERCODE = '{0}' )" +
						" or (MpmProjectDemand.state != 0 AND MpmProjectDemand.state != 2 ) " +
						"Then 1 else 0 END)=1 ",this.getUserBean().getUser().getCode());
			} else // 使用数据权限
			{
				sql.appendSingle(" and {0} ", dataPession);
			}
		}
		return sql.toString();
	}

	/**
	 * @author: houyuhang
	 * @date: 2021-08-04
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	@GetMapping(value = "/refList")
	public ResponseResult<Object> refList(@RequestParam(required = false) Map<String, Object> paraMap) {
		return super.refList(paraMap);
	}

	/****
	 *
	 * @description: 提交审批前
	 * @param dataMap
	 * @param paraMap
	 * @throws Exception
	 * @author: houyuhang
	 * @date: 2021-08-04
	 * @modify:
	 */
	@Override
	protected void beforeSubmit(Map<String, Object> dataMap, Map<String, Object> paraMap) throws Exception {
		super.beforeSubmit(dataMap, paraMap);
		StringBufferProxy mailtitle = new StringBufferProxy();
		mailtitle.appendSingle("{0}{1}于{2}提交项目需求申请,单据编号{3}! ", dataMap.get("dispdeptcode"), dataMap.get("username"), DateUtil.dateToString(new Date(), "yyyy-MM-dd"), dataMap.get("docno"));
		dataMap.put("maintitle", mailtitle.toString());
	}
	/****
	 *
	 * @description: 重写dataCodeList
	 * @param paraMap
	 * @author: houyuhang
	 * @date: 2021-08-09
	 * @modify:
	 */
	@Override
	@GetMapping(value = "/dataCodeList")
	public ResponseResult<Object> dataCodeList(@RequestParam(required = false) Map<String, Object> paraMap) {
		ResponseResult<Object> temp = super.dataCodeList(paraMap);
		Map<String,List<Map<String, Object>>> codeMap = (Map<String,List<Map<String, Object>>>)temp.getResult();
		List<Map<String, Object>> projectlevelList = datacodeCache.getCodeListMap("MpmProjectLevel", "", "sortcode");
		codeMap.put("projectlevelList", projectlevelList);
		return ResponseResult.ok(codeMap);
	}

	/****
	 *
	 * @description: 表单状态为不同意时清空分发人员工号distribute
	 * @param paraMap
	 * @author: houyuhang
	 * @date: 2021-08-25
	 * @modify:
	 */
	@PutMapping("/updateDistribute")
	public ResponseResult<Object> updateDistribute(@RequestBody(required = false) Map<String,Object> paraMap){
		List<String> list = new ArrayList<>();
		list.add(MpmProjectDemandEntity.FieldDistribute);
		service.updateFields(paraMap,list);
		return ResponseResult.ok(paraMap);
	}
	/****
	 *
	 * @description: 判断是否为超级管理员
	 * @author: houyuhang
	 * @date: 2021-09-07
	 * @modify:
	 */
	@GetMapping("/isAdmin")
	public ResponseResult<Object> isAdmin(){
		if(this.getUserBean().getUser().getAdminlevel() >= 1){
			return ResponseResult.ok(true);
		} else {
			return ResponseResult.ok(false);
		}
	}
}

